﻿using System;
using System.Windows.Input;
//#JMJ Create_20240527

namespace IHateWorking.Model
{
    public class CommandBase : ICommand
    {
        private readonly Action<object> _executeCommand = null;
        private readonly Func<object, bool> _canExecuteCommand = null;

        public CommandBase(Action<object> executeMethod)
            : this(executeMethod, null)
        { }

        public CommandBase(Action executeMethod)
        { }

        public CommandBase(CommandBase changeFilterTypeCommand)
        {
        }

        public CommandBase(Action<object> executeCommand, Func<object, bool> canExecuteCommand)
        {
            _executeCommand = executeCommand ?? throw new ArgumentNullException("executeMethod");
            _canExecuteCommand = canExecuteCommand;
        }

        event EventHandler ICommand.CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecuteCommand != null)
            {
                return _canExecuteCommand(parameter);
            }
            return true;
        }

        public void Execute(object parameter)
        {
            _executeCommand?.Invoke(parameter);
        }
    }
}
